#include <stdio.h>
#include <stdlib.h>
#include <vip_lite.h>

unsigned int get_file_size(const char *name)
{
    FILE *fp = fopen(name, "rb");
    unsigned int size = 0;

    if (fp != NULL)
    {
        fseek(fp, 0, SEEK_END);
        size = ftell(fp);

        fclose(fp);
    }
    else
    {
        printf("Checking file %s failed.\n", name);
    }

    return size;
}

unsigned int load_file(const char *name, void *dst)
{
    FILE *fp = fopen(name, "rb");
    unsigned int size = 0;

    if (fp != NULL)
    {
        fseek(fp, 0, SEEK_END);
        size = ftell(fp);

        fseek(fp, 0, SEEK_SET);
        size = fread(dst, size, 1, fp);

        fclose(fp);
    }

    return size;
}


int main(int argc, char **argv){
    vip_status_e status = VIP_SUCCESS;
    vip_network network = VIP_NULL;

    vip_buffer *input_buffer;
    vip_buffer *output_buffer;

    unsigned int bin_size;
    const char *bin_name = "wandModel.nb";
    unsigned char *bin_data;

    FILE *csvFile;
    const char *csvFileName = "target.csv";
    int csvData[130][3];
    int i, j;

    void *buf_data;

    printf("VIP test program starting...\n");

    printf("vip driver Version = 0x%08x\n", vip_get_version());

    // 初始化 NPU
    status = vip_init(10*128*3);
    if(status != VIP_SUCCESS){
        printf("vip init failed: %d\n", status);
        return status;
    }printf("vip init OK!\n");

    // 读取 NB 模型文件
    bin_size = get_file_size(bin_name);
    if(bin_size == 0){
        printf("Get NB file size failed!\n");
        return -1;
    }
    printf("NB File(%s) size: %d Bytes, equivalent to %d KB\n", bin_name, bin_size, bin_size/1024);

    bin_data = (unsigned char *)malloc(bin_size * sizeof(unsigned char));
    if (bin_data == VIP_NULL) {
        printf("No enough memory to load binary file.\n");
        vip_destroy();
        return -1;
    }

    printf("Loading binary ...\n");
    load_file(bin_name, (void *)bin_data);
    //Load the NBG data.
    status = vip_create_network(bin_data, bin_size, VIP_CREATE_NETWORK_FROM_MEMORY,&network);
    free(bin_data);
    if(status < 0){
        printf("Network creation failed: %d\n", status);
        return status;
    }

    // InitBuffers(network);
    printf("Preparing network object ... \n");
    status = vip_prepare_network(network);
    if(status < 0){
        printf("Error preparing network: %d\n", status);
        return status;
    }

    // 获取挥棒数据文件内容
    csvFile = fopen(csvFileName, "r");
    if (csvFile == NULL) {  
        printf("无法打开 csv 文件: %s\n", csvFileName);  
        return -1;  
    }
    // 读取数据到数组中  
    for (i = 0; i < 128; i++) {  
        for (j = 0; j < 3; j++) {  
            if (fscanf(csvFile, "%d,", &csvData[i][j]) != 1) {  
                printf("读取数据时出错\n");  
                return -1;  
            }  
        }  
        // 跳过行尾的换行符  
        if (fgetc(csvFile) != '\n') {  
            printf("文件格式不正确\n");  
            // return -1;  
        }  
    }  
  
    // 关闭文件  
    fclose(csvFile);  
  
    // 打印数组以验证数据是否正确读取  
    for (i = 0; i < 128; i++) {  
        for (j = 0; j < 3; j++) {  
            printf("%d ", csvData[i][j]); // 根据需要更改打印格式  
        }  
        printf("\n");  
    }  

    //InitInOut();
    // for (i = 0; i < input_count; i++) {
    //     buf_data = vip_map_buffer(input_buffer[i]);
    // }



    // deinit
    vip_destroy();

    return 0;
}
